<template>
  <div class="tab-control">
    <div
      class="tab-control-item"
      v-for="(item, i) in titles"
      :key="i"
      @click="goStyle(i)"
      :class="{active: i === controlIndex }"> 
      <!-- 根据当前最新索引值动态添加样式类 -->
      <span>{{ item }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabControl",
  data() {
    return {
      controlIndex: 0, // 当前索引值
    };
  },
  props: {
    titles: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  methods: {
    goStyle(i) {
      this.controlIndex = i;
      this.$emit('tabClick', i)
      // console.log(this.controlIndex);
    },
  },
};
</script>

<style scoped>
.tab-control {
  background-color: #fff;
  display: flex;
  text-align: center;
}
.tab-control-item {
  flex: 1;
  height: 40px;
  line-height: 40px;
}
.tab-control-item span {
  padding: 5px;
}
.active {
  color: #d43c9ccc ;
}
.active span {
  border-bottom: 3px solid #d43c9ccc;
}
</style>